<template>
  <van-dialog
    v-model="visible"
    show-cancel-button
    :beforeClose="handleSubmit"
    confirm-button-color="rgba(25, 137, 250,1)"
  >
    <div class="ip-container">
      <van-field label="物料号" :value="this.ShowDataA[0].ProductId" readonly />
      <van-field label="拍号" :value="this.ShowDataA[0].UniqueId" readonly />
      <van-field
        label="物料名称"
        :value="this.ShowDataA[0].Producer"
        readonly
      />
      <van-field
        label="调前数量"
        :value="this.ShowDataA[0].StockQuantity"
        readonly
      />
      <van-field
        v-model="savevalue"
        required
        label="调后数值"
        placeholder="请输入要修改的数值"
      />
    </div>
  </van-dialog>
</template>

<script>
import { Dialog } from "vant";
import { toast } from "../utils/common";

export default {
  components: {
    [Dialog.Component.name]: Dialog.Component,
  },
  watch: {
    value(val) {
      this.visible = val;
    },
    visible(val) {
      this.$emit("input", val);
    },
    Asure(val) {
      this.sure = val;
    },
    ShowData(val) {
      this.ShowDataA = val;
      console.log(this.ShowDataA);
    },
    AdjustStatue(val) {
      this.AdjustStatue = val;
      console.log(this.AdjustStatue);
    },
  },
  props: {
    //父给子传递
    value: {
      type: Boolean,
      required: true,
    },
    Asure: {
      type: Boolean,
      required: false,
    },
    chooseValue: {
      type: String,
    },
    ShowData: {
      type: Array,
    },
    AdjustStatue: {
      type: String,
    },
  },
  data() {
    return {
      savevalue: "",
      visible: this.value,
      sure: this.Asure,
      statue: "",
      ShowDataA: [
        {
          ProductId: "",
          UniqueId: "",
          StockQuantity: "",
          Producer: "",
        },
      ],
    };
  },
  methods: {
    handleSubmit(action, done) {
      if (action === "confirm") {
        this.AdjustChanged();
        if (this.statue == true) {
          this.sure = true;
          this.$emit("adjustNum", this.savevalue, this.sure);
          this.savevalue = "";
          done();
        } else {
          done(false);
        }
      } else {
        this.sure = false;
        this.$emit("canCel", this.sure);
        done();
      }
    },
    //切换状态
    AdjustChanged() {
      let newVal = Number(this.savevalue);
      if (!isNaN(newVal)) {
        switch (this.AdjustStatue) {
          case "D":
            if (this.ShowDataA[0].StockQuantity < newVal) {
              console.log("11");
              toast("新值不能大于旧值");
              this.statue = false;
              this.savevalue = "";
            } else if (newVal == "" || newVal == undefined || newVal == null) {
              this.statue = false;
              toast("请输入正确的数字");
            } else {
              this.statue = true;
              console.log(this.statue);
            }
            break;
          case "I":
            if (this.ShowDataA[0].StockQuantity > newVal) {
              toast("新值不能小于旧值");
              this.statue = false;
              this.savevalue = "";
            } else if (newVal == "" || newVal == undefined || newVal == null) {
              this.statue = false;
              toast("请输入正确的数字");
            } else {
              this.statue = true;
            }
            break;
          default:
            toast("数量调整选项配置有问题，请修改后台配置");
        }
      } else {
        toast("请输入正确的数字");
        this.statue = false;
        this.savevalue = "";
      }
    },
  },
  mounted() {
    //组件进来将父组件给的value值给visible
    this.visible = this.value;
  },
};
</script>

<style scoped lang="less">
.ip-container {
  padding: 1rem;
}
</style>